import 'package:flutter/material.dart';
import 'package:flutter_demo/routers/router_paths.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_riverpod/legacy.dart';
import 'package:go_router/go_router.dart';

final _navSelectedIndexProvider = StateProvider<int>((ref) => 0);

class CuNavigationBar extends ConsumerWidget {
  const CuNavigationBar({super.key});
  void _onTap(int index, BuildContext ctx, WidgetRef ref) {
    ref.read(_navSelectedIndexProvider.notifier).state = index;
    switch (index) {
      case 0:
        ctx.replaceNamed(RouterPaths.home);
        break;
      case 1:
        ctx.replaceNamed(RouterPaths.component);
        break;
      case 2:
        ctx.replaceNamed(RouterPaths.user);
        break;
    }
  }

  @override
  Widget build(BuildContext ctx, WidgetRef ref) {
    final selectIndex = ref.watch(_navSelectedIndexProvider);
    return BottomNavigationBar(
      currentIndex: selectIndex,
      onTap: (index) => _onTap(index, ctx, ref),
      selectedItemColor: Colors.blue,
      unselectedItemColor: Colors.grey,
      showUnselectedLabels: true,
      items: [
        BottomNavigationBarItem(icon: Icon(Icons.home), label: "主页"),
        BottomNavigationBarItem(icon: Icon(Icons.widgets), label: "组件"),
        BottomNavigationBarItem(icon: Icon(Icons.person), label: "用户"),
      ],
    );
  }
}
